package cn.fyl.system.controller;

import cn.fyl.basic.util.JsonResult;
import cn.fyl.basic.util.PageList;
import cn.fyl.system.domain.Permission;
import cn.fyl.system.query.PermissionQuery;
import cn.fyl.system.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *菜单管理接口
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermissionService PermissionService;



    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public List<Permission> findAll(){
        return PermissionService.loadAll();
    }

    /**
     * 查询一个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Permission findById(@PathVariable("id") Long id){
        return PermissionService.loadById(id);
    }


    /**
     * 添加和修改操作
     * @param Permission
     * @return
     */
    @PostMapping
    public JsonResult save(@RequestBody Permission Permission){
        try {
            if(Permission.getId()==null){
                PermissionService.save(Permission);
            }else {
                PermissionService.update(Permission);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }

    }

    /**
     * 分页查询
     * @param
     * @return
     */
    @PutMapping
    public PageList<Permission> queryPage(@RequestBody PermissionQuery query){
        try {
            return PermissionService.queryData(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<Permission>();
        }
    }



}
